<?php
class ProductsUsersController extends AppController {

	var $name = 'ProductsUsers';

    function beforeFilter () {
        parent::beforeFilter();

		// Aplico el Layout segun la Vista o si es AJAX
		if ($this->RequestHandler->isAjax()) {
			$this->layout = 'ajax';
		}else{
			// Cambio la Vista por la del Rol
			$this->layout = strtolower($this->Session->read('VistaActual'));
		}
    }
    
	function index() {
		$this->ProductsUser->recursive = 0;
		$this->set('productsUsers', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid products user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('productsUser', $this->ProductsUser->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->ProductsUser->create();
			if ($this->ProductsUser->save($this->data)) {
				$this->Session->setFlash(__('The products user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The products user could not be saved. Please, try again.', true));
			}
		}
		$products = $this->ProductsUser->Product->find('list');
		$users = $this->ProductsUser->User->find('list');
		$this->set(compact('products', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid products user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->ProductsUser->save($this->data)) {
				$this->Session->setFlash(__('The products user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The products user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ProductsUser->read(null, $id);
		}
		$products = $this->ProductsUser->Product->find('list');
		$users = $this->ProductsUser->User->find('list');
		$this->set(compact('products', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for products user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ProductsUser->delete($id)) {
			$this->Session->setFlash(__('Products user deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Products user was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>